{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def calc_map_from_cnn_features(solver, test_iterations, metric):\n",
    "    net_output = np.zeros(\n",
    "        (test_iterations, solver.test_nets[0].blobs[\"sigmoid\"].data.flatten().shape[0])\n",
    "    )\n",
    "    labels = np.zeros(test_iterations)\n",
    "    for idx in xrange(solver.param.test_iter[0]):\n",
    "        # calculate the net output\n",
    "        solver.test_nets[0].forward()\n",
    "\n",
    "        net_output[idx] = solver.test_nets[0].blobs[\"sigmoid\"].data.flatten()\n",
    "        labels[idx] = solver.test_nets[0].blobs[\"label\"].data.flatten()\n",
    "    # calculate mAP\n",
    "    _, ave_precs = map_from_feature_matrix(\n",
    "        features=net_output, labels=labels, metric=metric, drop_first=True\n",
    "    )\n",
    "    # some queries might not have a relevant sample in the test set\n",
    "    # -> exclude them\n",
    "    mean_ap = np.mean(ave_precs[ave_precs > 0])\n",
    "    return mean_ap, ave_precs"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
